package com.ds.infrastructure.hyperspace.container.admin.processor;

import com.ds.infrastructure.hyperspace.common.admin.AdminResponseVO;
import com.ds.infrastructure.hyperspace.common.admin.GzsStatusInputVO;
import com.ds.infrastructure.hyperspace.container.internal.NettyContainer;
import com.ds.infrastructure.hyperspace.container.internal.gzs.GzsService;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;

/**
 * @author  Weishaoying
 */
@Slf4j
public class GzsStatusProcessor implements Processor {

    private final GzsService gzsService;

    public GzsStatusProcessor(GzsService gzsService) {
        this.gzsService = gzsService;
    }

    @Override
    public AdminResponseVO process(NettyContainer container, byte[] msg) throws IOException {
        AdminResponseVO result = new AdminResponseVO();
        if (gzsService == null) {
            result.setCode(1);
            result.setMsg("Gzs service not init!");
            return result;
        }
        GzsStatusInputVO gzsStatusInputVO = NettyContainer.OBJECT_MAPPER.readValue(msg, GzsStatusInputVO.class);
        result.setMsg(String.valueOf(gzsService.getZone(gzsStatusInputVO.getShardingKeyType(),
                gzsStatusInputVO.getShardingKey())));
        return result;
    }

}
